// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;
import ButtonEx from "./ButtonEx";

@ccclass
export default class ButtonExGroup extends cc.Component {

    @property([ButtonEx])
    checkNodes: ButtonEx[] = [];

    @property
    indexOnLoad:boolean = true;

    onLoad () {
        this.node.on("ButtonEx",this.buttonExEvent,this)
        if (this.indexOnLoad && this.checkNodes.length > 0) {
            this.checkButtonEx(this.checkNodes[0]);
        }
    }

    buttonExEvent (event) {
        if (event && event.target) {
            this.checkButtonEx (event.target);
        }
    }

    checkButtonEx ( btnEx, eventFlag = true) {
        for (let index = 0; index < this.checkNodes.length; index++) {
            const element = this.checkNodes[index];
            if (btnEx == element) {
                element.setChecked(true);
                if (eventFlag) {
                    this.node.emit("ButtonExGroup",{index: index})
                }
            }else {
                element.setChecked(false);
            }
        }
    }

    setCheckIndex(index, eventFlag = true) {
        this.checkButtonEx(this.checkNodes[index], eventFlag);
    }
}
